//
//  SwipeToViewController.m
//  AnimationDemo
//
//  Created by Jiaozl 2018 on 2018/9/14.
//  Copyright © 2018年 Jiaozl. All rights reserved.
//

#import "SwipeToViewController.h"
#import "ZLTransitionDelegate.h"
#import "ZLInteractiveTransition.h"
#import "SwipeTransitionAnimation.h"
@interface SwipeToViewController ()
@property (nonatomic, strong) ZLTransitionDelegate *transitionDelegate;
@property (nonatomic, strong) SwipeTransitionAnimation *swipeTransition;
@end

@implementation SwipeToViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.transitioningDelegate = self.transitionDelegate;
        self.modalPresentationStyle = UIModalPresentationFullScreen;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = UIColor.yellowColor;
    [self setupGesture];
}

- (void)setupGesture
{
    // 添加屏幕边缘滑动手势
    UIScreenEdgePanGestureRecognizer *screenEdgePanGR = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(__actionLeftSwipe:)];
    screenEdgePanGR.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePanGR];
}

// MARK: - action
- (IBAction)back:(UIButton *)sender
{
    [self __actionPresentNewVC:sender];
}

- (void)__actionLeftSwipe:(UIScreenEdgePanGestureRecognizer *)sender
{
    [self __actionPresentNewVC:sender];
}

- (void)__actionPresentNewVC:(id)sender
{
    if ([sender isKindOfClass:UIGestureRecognizer.class] ) {
        if (((UIGestureRecognizer *)sender).state == UIGestureRecognizerStateBegan) {
            [self __dismissVC:sender];
        }
    } else {
        [self __dismissVC:sender];
    }
}

- (void)__dismissVC:(id)sender
{
    if ([sender isKindOfClass:UIGestureRecognizer.class] ) {
        self.gesture = sender;
    } else {
        self.gesture = nil;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)navigationShouldPopOnBackButton
{
    self.gesture = nil;
    return YES;
}

// MARK: - getter
- (ZLTransitionDelegate *)transitionDelegate
{
    if (!_transitionDelegate) {
        _transitionDelegate = [[ZLTransitionDelegate alloc] init];
        weakify(self);
        _transitionDelegate.animatedPresentTransition = ^id<UIViewControllerAnimatedTransitioning> _Nonnull{
            strongify(self);
            self.swipeTransition.targetEdge = UIRectEdgeRight;
            return self.swipeTransition;
        };
        _transitionDelegate.animatedDismissalTransition = ^id<UIViewControllerAnimatedTransitioning> _Nonnull{
            strongify(self);
            self.swipeTransition.targetEdge = UIRectEdgeLeft;
            return self.swipeTransition;
        };
        _transitionDelegate.interactivePresentTransition = ^id<UIViewControllerInteractiveTransitioning> _Nonnull{
            strongify(self);
            if (self.gesture) {
                return [[ZLInteractiveTransition alloc] initWithGestureRecognizer:self.gesture edgeForDragging:UIRectEdgeRight];
            }
            return nil;
        };
        _transitionDelegate.interactiveDismissalTransition = ^id<UIViewControllerInteractiveTransitioning> _Nonnull{
            strongify(self);
            if (self.gesture) {
                return [[ZLInteractiveTransition alloc] initWithGestureRecognizer:self.gesture edgeForDragging:UIRectEdgeLeft];
            }
            return nil;
        };
    }
    return _transitionDelegate;
}

- (SwipeTransitionAnimation *)swipeTransition
{
    if (!_swipeTransition) {
        _swipeTransition = [SwipeTransitionAnimation animatedTransition];
    }
    return _swipeTransition;
}

@end
